<template>
<!-- dblclick双击 -->
  <div class="my-tag" @dblclick="open">
    <input
      class="input"
      type="text"
      placeholder="输入标签"
      v-if="edit"
      v-focus
      @blur="close"
      @keydown.enter="close"
      v-model.trim="tag"
    />
    <div v-else class="text">{{ value }}</div>
  </div>
</template>

<script>
export default {
  props: ['value'],
  data() {
    return {
      edit: false, //表示编辑状态
      tag: '',
    }
  },
  methods: {
    open() {
      console.log('openEdit')
      this.edit = true
      this.tag = this.value
    },
    //失去焦点时隐藏input
    // @blur="close"
    close() {
      this.edit = false
      this.$emit('input',this.tag)
    },
  },
  //   定义自定义指令
  directives: {
    // input框显示时自动获取焦点
    focus: {
      inserted(el) {
        el.focus()
      },
    },
  },
}
</script>

<style lang="less">
.my-tag {
  cursor: pointer;
  .input {
    appearance: none;
    outline: none;
    border: 1px solid #ccc;
    width: 100px;
    height: 40px;
    box-sizing: border-box;
    padding: 10px;
    color: #666;
    &::placeholder {
      color: #666;
    }
  }
}
</style>
